package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    //@Autowired
    //DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    /*@Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);

        Long dishId = dishDto.getId();
        List<DishFlavor> dishFlavors = dishDto.getFlavors();
        dishFlavors=dishFlavors.stream().map((item) ->{
            item.setDishId(dishId);
            return  item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(dishFlavors);

    }*/

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品数据
        dishMapper.insert(dishDto);

        //判断是否选择了口味
        if(dishDto.getFlavors()!= null && dishDto.getFlavors().size() >0){
            Long id = dishDto.getId();
            for (DishFlavor flavor : dishDto.getFlavors()) {
                flavor.setDishId(id);
                dishFlavorMapper.insert(flavor);
            }
        }
    }
}
